<?php
    /**
     * Created by PhpStorm.
     * 版权所有: 2019~2022 [ hhygyl ]
     * Date: 2019/7/25-15:58
     * Link: http://luckyadmin.luckyhhy.cn
     * FileName: Feedback.php
     * Keys: ctrl+alt+L/ctrl+s(代码格式化) ctrl+J(代码提示) ctrl+R(替换)ALT+INSERT(生成代码(如GET,SET方法,构造函数等) , 光标在类中才生效)
     * CTRL+ALT+O (优化导入的类和包 需要配置) SHIFT+F2(高亮错误或警告快速定位错误)
     * CTRL+SHIFT+Z(代码向前) CTRL+SHIFT+/ (块状注释) ctrl+shift+enter(智能完善代码 如if())
     *
     **************************************************************
     *                                                            *
     *   .=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-.       *
     *    |                     ______                     |      *
     *    |                  .-"      "-.                  |      *
     *    |                 /            \                 |      *
     *    |     _          |              |          _     |      *
     *    |    ( \         |,  .-.  .-.  ,|         / )    |      *
     *    |     > "=._     | )(__/  \__)( |     _.=" <     |      *
     *    |    (_/"=._"=._ |/     /\     \| _.="_.="\_)    |      *
     *    |           "=._"(_     ^^     _)"_.="           |      *
     *    |               "=\__|IIIIII|__/="               |      *
     *    |              _.="| \IIIIII/ |"=._              |      *
     *    |    _     _.="_.="\          /"=._"=._     _    |      *
     *    |   ( \_.="_.="     `--------`     "=._"=._/ )   |      *
     *    |    > _.="                            "=._ <    |      *
     *    |   (_/                                    \_)   |      *
     *    |                                                |      *
     *    '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-='      *
     *                                                            *
     *                    JUST FIND YOUR BUG !                    *
     **************************************************************
     */

    namespace app\admin\controller;


    use service\UtilService;
    use think\Exception;

    class Feedback extends SystemBase
    {
        protected $feedback;
        protected function _initialize()
        {
            parent::_initialize(); // TODO: Change the autogenerated stub
            $this->feedback=new \app\admin\model\Feedback();
        }



        /**
         * @return mixed
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: index
         * @describe:数据展示
         */
        public function index(){
            if($this->request->isAjax()){
                $param=$this->request->param();
                return $this->feedback->GetFeedbackDataPage($param);
            }
            return $this->fetch();
        }


        /**
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: delete
         * @describe:删除
         */
        public function delete(){
            if($this->request->isAjax()) {
                $ids = $this->request->post("ids");
                return $this->DeleteData("feedback",$ids);
            }
        }



        /**
         * @author: hhygyl <hhygyl520@qq.com>
         * @name: check_ok
         * @describe:留言审核
         */
        public function check_ok(){
            if($this->request->isAjax()){
                $ids=$this->request->post("ids");
                try{
                    $res=$this->feedback->check_ok($ids);
                    if($res!==false){
                        $this->AddLogs("审核留言ID【".$ids."】");
                        $this->success("审核成功");
                    }else{
                        $this->error("审核失败");
                    }
                }catch (Exception $exception){
                    $this->error($exception->getMessage());
                }
            }
        }


        /**
         * @throws \phpmailerException
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: replay
         * @describe:回复留言
         */
        public function replay(){
            if($this->request->isAjax()){
                $data=$this->request->post();
                $data['username']=self::$admin_info['nickname'];
                $data['img']=self::$admin_info['avatar'];
                $data['email']=self::$admin_info['email'];
                $data['status']=1;
                    $res=$this->feedback->addEditData($data);
                    if($res['code']==1){
                        $info=$this->feedback->get($data['pid']);
                        if($info){
                            $ok=$this->send_mail($data['pid'],$data['content']);
                            if($ok==false){
                                $this->error("留言回复成功！邮件通知发送失败！");
                            }
                        }
                        $this->success("回复成功");
                    }else{
                        $this->error($res['msg']);
                    }

            }
        }


        /**
         * @param $id
         * @param $replay
         * @return bool
         * @throws \phpmailerException
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: hhygyl <jackhhy520@qq.com>
         * @name: send_mail
         * @describe:回复留言
         */
        protected function send_mail($id,$replay){
            $yuan=$this->feedback->get($id)->toArray();
            $info=$this->email_tem->get(['tag'=>'liuyan','status'=>1])->toArray();

            if(!$info){
                return false;
            }

            $arr=["username","content","replay","url","blog"];
            $need=[$yuan['username'],$yuan['content'],$replay,"<a href='http://www.jackhhy.cn/feedback/42.html' target='_blank'>链接</a>","<a href='http://www.jackhhy.cn' target='_blank'>小贺博客</a>"];
            foreach($arr as $k=>$v){
                $info['content']=str_replace($v,$need[$k],$info['content']);
            }

            $email=UtilService::is_email($yuan['email']);
            if(!$email){
                return false;
            }

            $ok=$this->Send_Maile("小贺个人博客留言回复提醒",$info['content'],$yuan['email']);
            if($ok){
                db("email_sends")->insert([
                    'email'=>$yuan['email'],
                    'title'=>"小贺个人博客留言回复提醒",
                    'content'=>$info['content'],
                    'nickname' =>$yuan['username'],
                    'ok'=>"发送成功",
                    'create_time'=>date("Y-m-d H:i:s",time())
                ]);
               return true;
            }else{
                return false;
            }
        }


    }